<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/xmltojson.js"></script>
<script type="text/javascript">
    JSON.stringify = JSON.stringify || function (obj) {
            var t = typeof (obj);
            if (t != "object" || obj === null) {
                // simple data type
                if (t == "string") obj = '"'+obj+'"';
                return String(obj);
            }
            else {
                // recurse array or object
                var n, v, json = [], arr = (obj && obj.constructor == Array);
                for (n in obj) {
                    v = obj[n]; t = typeof(v);
                    if (t == "string") v = '"'+v+'"';
                    else if (t == "object" && v !== null) v = JSON.stringify(v);
                    json.push((arr ? "" : '"' + n + '":') + String(v));
                }
                return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
            }
        };
    function xmlToString(xmlData) { 
        var xmlString;
        //IE
        if (window.ActiveXObject){
            xmlString = xmlData.xml;
        }
        // code for Mozilla, Firefox, Opera, etc.
        else{
            xmlString = (new XMLSerializer()).serializeToString(xmlData);
        }
        return xmlString;
    } 
    function loadHistory()
    {
        $.ajax({
        type: "GET",
        url: "http://playerservices.streamtheworld.com/public/nowplaying?mountName=<?php echo $station_id ?>&numberToFetch=10&eventType=track",
        dataType: "xml",
        success: function(xml) {
            var xmlString = xmlToString(xml);
            var json = $.xml2json(xmlString);
            var htm = '<ul style="list-style:none;">';
            for(i in json.nowplaying_info)
            {    
                now_playing = json.nowplaying_info[i].property;
                if(i!=0)
                {
                    album = now_playing[0];
                    artist = now_playing[1];
                    title = now_playing[2];
                    htm+= '<li>'+artist+' - '+title+'<span class="pull-right"><a rel="'+artist+'-'+title+'" class="request btn">request</a></span></li>';
                }
                else
                {
                    $('#now-playing-album').val(now_playing[0]);
                    $('#now-playing-artist').val(now_playing[1]);
                    $('#now-playing-title').val(now_playing[2]);
                }
            }
            htm+= '</ul>';
            $('#song-history').html(htm);
        }
        });    
    
    }  
    $(document).ready(function(){
        loadHistory();
        setInterval("loadHistory()",10000);
        
        $('.player-tooltip').tooltip();
       
        $(".request").live('click',function(e){
            e.preventDefault();
            data = {song : $(this).attr('rel')};
            $.ajax({
                'url':'<?php echo base_url(); ?>player/process_song_request/<?php echo $community; ?>',
                'type':'post',
                'dataType':'json',
                'data':data,
                'success':function(data){
                    //alert(data.message);
                    if(data.message == 'error'){
                        alert('You need to be logged in to request a song');
                       document.location = '<?php echo base_url(); ?>login';                        
                    }
                    else if(data.message == 'not expired')
                    {
                        alert('You have already requested that song. You can only request the same song after 24 hours');
                    }
                    else
                    {
                        alert('Your song has been sent as a request! Thanks');
                    }
                }
            })
        });
        $('.collapse').collapse({toggle:false});
        $('.collapse').on('hidden', function () { 
            $(this).parent().find('img').attr('src','<?php echo $img_dir; ?>open-arrow.png');
        });
         $('.collapse').on('shown', function () {
               $(this).parent().find('img').attr('src','<?php echo $img_dir; ?>close-arrow.png');
        });
        
        $('a.fb-music').click(function(e){
            var artist = $('#now-playing-artist').val();
            var song = $('#now-playing-title').val();
            var album = $('#now-playing-album').val();
            e.preventDefault();
            FB.ui({
              method: 'feed',
              picture:'<?php echo IMAGE_UPLOAD_DIR.$community_image; ?>',    
              link: '<?php echo current_url(); ?>',
              name: '<?php echo $community; ?>',
              caption: 'I\'m Listening to '+song+' by '+artist+' on <?php echo $community; ?>',
              description: 'Pinoytuner.com'
            }, function(response){
                //response = {"post_id":"userID_postObjectID"};
                //separate on "_"
            });
        });
        $('a.fb-video').click(function(e){
            var title = $('#video-title').val();
            var desc = $('#video-desc').val();
            e.preventDefault();
            FB.ui({
              method: 'feed',
              picture:'<?php echo base_url() ?>assets/lib/uploader/server/php/files/<?php echo $community_image; ?>',    
              link: '<?php echo current_url(); ?>',
              name: title+' live on <?php echo $community; ?>',
              caption: desc,
              description: 'Pinoytuner.com'
            }, function(response){
                //response = {"post_id":"userID_postObjectID"};
                //separate on "_"
            });
        });
        
         $('a.twitter-video').click(function(e){
            var title = $('#video-title').val();
            var desc = $('#video-desc').val();
            window.open('http://twitter.com/intent/tweet?text=I%27m%watching%20to%20'+title+'%20on%20<?php echo $community; ?>%20@pinoytuner%20<?php echo urlencode(current_url()); ?>&hashtags=pinoytuner,<?php echo $community; ?>');
        });
        
        $('a.twitter-music').click(function(e){
            var artist = $('#now-playing-artist').val();
            var song = $('#now-playing-title').val();
            var album = $('#now-playing-album').val();
            window.open('http://twitter.com/intent/tweet?text=I%27m%20listening%20to%20'+song+'%20by%20'+'%20'+artist+'%20on%20<?php echo $community; ?>%20@pinoytuner%20<?php echo urlencode(current_url()); ?>&hashtags=pinoytuner,<?php echo $community; ?>');
        });
        
    
    });
</script>
<!-------google analytics---------------------->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-18317479-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>